package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcOldmanPaidExpenseItemMapper;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.dto.old_out.OldmanPaidQueryDto;
import com.csun.cmny.provider.model.vo.OmcOldmanPaidItemVo;
import com.csun.cmny.provider.model.vo.revise.OldmanPaymentFeeVo;
import com.csun.cmny.provider.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Transactional
@Service
public class OmcOldmanPaidExpenseItemServiceImpl extends BaseService<OmcOldmanPaidExpenseItem>
        implements OmcOldmanPaidExpenseItemService {

    @Resource
    private OmcOldmanPaidExpenseItemMapper omcOldmanPaidExpenseItemMapper;

    @Resource
    private OmcOldManBillService omcOldManBillService;

    @Resource
    private OmcBillMealService omcBillMealService;

    @Resource
    private OmcSetMealService omcSetMealService;

    @Resource
    private OmcBillSetMealItemService omcBillSetMealItemService;

    @Override
    public OmcOldmanPaidItemVo queryPaidItemByOldmanId(Long oldmanId, LoginAuthDto loginAuthDto) {

        //定义返回结果集
        OmcOldmanPaidItemVo vo = new OmcOldmanPaidItemVo();

        //查找入院缴费单
        OmcOldManBill billQuery = new OmcOldManBill();
        billQuery.setOldManId(oldmanId);
        billQuery.setBillType(1);
        List<OmcOldManBill> omcOldManBills = omcOldManBillService.select(billQuery);

        /*
         * 2020-07-28
         * xcr
         * 二次入院-选择最新入院账单的信息
         * */
        if(omcOldManBills != null && omcOldManBills.size() > 0){
            billQuery = omcOldManBills.get(0);
            for (OmcOldManBill bill : omcOldManBills) {
                if(bill.getCreatedTime().compareTo(billQuery.getCreatedTime()) > 0){
                    billQuery = bill;
                }
            }
        }

        if(billQuery != null){
            //查找入院购买的套餐
            OmcBillMeal billMealQuery = new OmcBillMeal();
            billMealQuery.setBillId(billQuery.getId());
            billMealQuery = omcBillMealService.selectOne(billMealQuery);
            if(billMealQuery != null){
                OmcBillSetMealItem billSetMealItemQuery = new OmcBillSetMealItem();
                billSetMealItemQuery.setSetmealId(billMealQuery.getSetMealId());
                billSetMealItemQuery.setBillId(billQuery.getId());
                List<OmcBillSetMealItem> billSetMealItemList = omcBillSetMealItemService.select(billSetMealItemQuery);
                billMealQuery.setBillSetMealItemList(billSetMealItemList);
                vo.setSetMeal(billMealQuery);
            }
        }

        //查询需要支付的项目
        OmcOldmanPaidExpenseItem query = new OmcOldmanPaidExpenseItem();
        query.setOldmanId(oldmanId);
        List<OmcOldmanPaidExpenseItem> list = select(query);

        //将需要支付的项目存入返回结果集
        if(list != null && list.size() >0){
            vo.setItemList(list);
        }
        return vo;
    }

    @Override
    public int batchSaveOldmanPaidExpenseItem(List<OmcOldmanPaidExpenseItem> oldmanPaidExpenseItemList) {
        OmcOldmanPaidExpenseItem deleteItem = new OmcOldmanPaidExpenseItem();
        deleteItem.setOldmanId(oldmanPaidExpenseItemList.get(0).getOldmanId());
        int retDelete = delete(deleteItem);
        return batchSave(oldmanPaidExpenseItemList);
    }

    /**
     * query oldman check in payment paid item list
     *
     * @param oldmanPaidQueryDto the oldman paid query dto
     *
     * @return the list.
     */
    @Override
    public List<OldmanPaymentFeeVo> queryPaidItemList(OldmanPaidQueryDto oldmanPaidQueryDto) {

        return omcOldmanPaidExpenseItemMapper.queryPaidItemList(oldmanPaidQueryDto);
    }
}
